#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os, re
import logging
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    # import pymel.core as pm
    import JTK.jmaya.core as jmc

except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CheckEdgeProblem(CheckBase):
    def __init__(self):
        super(CheckEdgeProblem, self).__init__()
        pass

    def check_edges_faceConnection(self):
        error_edges = []
        for mesh in cmds.ls(type="mesh"):
            for edge_index in range(cmds.polyEvaluate(mesh, e=1)):
                edge_string = "{0}.e[{1}]".format(mesh, edge_index)
                connection_face = cmds.polyInfo(edge_string, ef=1)[0]
                face_indexes = self.get_index_from_string(connection_face)
                if len(face_indexes) > 2:
                    error_edges.append(edge_string)
        return error_edges

    def get_index_from_string(self, info_string):
        info_string = info_string.split(":")[1]

        indexes = []
        if re.findall("(\d+)", info_string):
            for index in re.findall("(\d+)", info_string):
                indexes.append(index)
        return indexes

    def check(self):
        result_proc = self.check_edges_faceConnection()
        if result_proc:
            result_log = u'Error:%s\n' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        pass
